package com.project.running.web.Filter;

import com.project.running.Service.Impl.LoginServiceImpl;
import com.project.running.Service.LoginService;
import com.project.running.bean.Memberinfo;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/6-10-06-16:33
 * @Description：com.project.running.web.Filter
 */

//@WebFilter(value = "/*", initParams = {@WebInitParam(name="encoding", value = "utf-8")  })
public class CheckLogin implements Filter {
    String encoding;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding= filterConfig.getInitParameter("encoding");
    }
    @Autowired
    LoginService service;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        Memberinfo user = (Memberinfo) request.getSession().getAttribute("memberInfo");

        if((request.getRequestURL().toString().contains("getVerifyCode") || user != null || request.getRequestURL().toString().endsWith(".gif"))&&!request.getRequestURL().toString().contains("login")){     //如果是验证码或者已登录，则放行
            filterChain.doFilter(request,response);//放行
            return;
        }


        Cookie[] cookies = request.getCookies();
        String username = null;
        String password = null;
//        System.out.println(cookies);
        Memberinfo memberinfo = null;
        if(cookies != null && cookies.length > 0){          //没有cookie，则需要登录//判断是否有用户名和密码
            for (Cookie cookie : cookies) {
                if("username".equals(cookie.getName()) ){
                    username = cookie.getValue();
                }
                if(cookie.getName().equals("password")){
                    password = cookie.getValue();
                }
            }

            if(username != null && password != null){               //查询到用户名和密码
                memberinfo = service.login(username, password);
            }
            if(memberinfo != null){                            //登录成功情况
                request.getSession().setAttribute("memberInfo",memberinfo);       //将用户名存入session
            }

        }
        if(!request.getRequestURL().toString().contains("login")&&memberinfo == null){
            response.sendRedirect("/Running/login.jsp");
            return;
        }else if(request.getRequestURL().toString().contains("login")&&memberinfo != null){
            response.sendRedirect("member/activity.jsp");
            return;
        }
        filterChain.doFilter(request,response);
    }
    @Override
    public void destroy() {

    }
}
